import request from "@/utils/request";
import Qs from "qs";

//用户管理
export function getAllUser(currentPage, pageSize) {
    return new Promise((resolve, reject) => {
        request.get("user/all", {
            params: {
                currentPage,
                pageSize
            }
        }).then(resp => {
            resolve(resp.data)
        }).catch(error => {
            reject(error)
        })
    });
}

export function addUser(username, password, role) {
    return new Promise((resolve, reject) => {
        request.post("user/register", Qs.stringify({username, password, role})).then(resp => {
            resolve(resp.data)
        }).catch(error => {
            reject(error)
        })
    });
}

export function getUserById(id) {
    return new Promise((resolve, reject) => {
        request.get("user/get/" + id).then(resp => {
            resolve(resp.data)
        }).catch(error => {
            reject(error)
        })
    });
}

export function deleteUserById(id) {
    return new Promise((resolve, reject) => {
        request.delete("user/delete/" + id).then(resp => {
            resolve(resp.data)
        }).catch(error => {
            reject(error)
        })
    });
}

export function updateUserById(user) {
    return new Promise((resolve, reject) => {
        request.post("user/update/", Qs.stringify({
            id: user.id,
            username: user.username,
            trueName: user.trueName,
            gender: user.gender,
            password: user.password,
            address: user.address,
            roleId: user.roleId,
            departmentId: user.department
        })).then(resp => {
            resolve(resp.data)
        }).catch(error => {
            reject(error)
        })
    });
}


export function changeStatusById(id, status) {
    return new Promise((resolve, reject) => {
        request.post("user/status", Qs.stringify({
            id,
            status
        })).then(resp => {
            resolve(resp.data)
        }).catch(error => {
            reject(error)
        })
    });
}

export function fuzzyQuery(username) {
    return new Promise((resolve, reject) => {
        request.get("user/fuzzy/query", {
            params: {
                input: username,
            }
        }).then(resp => {
            resolve(resp.data)
        }).catch(error => {
            reject(error)
        })
    });
}

export function getAllDepartment() {
    return new Promise((resolve, reject) => {
        request.get("department/query/all").then(resp => {
            resolve(resp.data)
        }).catch(error => {
            reject(error)
        })
    });
}

export function addDepartment(depart) {
    return new Promise((resolve, reject) => {
        request.get("department/add/department", {params: {department: depart}}).then(resp => {
            resolve(resp.data)
        }).catch(error => {
            reject(error)
        })
    });
}

export function deleteDepartment(id) {
    return new Promise((resolve, reject) => {
        request.delete("/department/delete/" + id).then(resp => {
            resolve(resp.data)
        }).catch(error => {
            reject(error)
        })
    });
}